package com.album.activity.adapter;

 
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;  
import java.util.Vector;

import com.album.R;
import com.album.common.Constants;
import com.album.utils.MyLog;
   

import android.content.Context;  
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.RelativeLayout;
import android.widget.TextView;  
  
/** 
 * Copyright (C) 2010,Under the supervision of China Telecom Corporation Limited 
 * Guangdong Research Institute The New Vphone Project 
 *  
 * @Author fonter.yang 
 * @Create date：2010-10-11 
 *  
 */  
public class GridAdapter extends BaseAdapter {  
  
    private class GridHolder {  
        ImageView appImage;   
    }  
  
    private Context context;  
  
    private LayoutInflater mInflater;  

    private Vector<SoftReference<Bitmap>> list =new Vector<SoftReference<Bitmap>>();
 
    public GridAdapter(Context c,Vector<SoftReference<Bitmap>> datalist) {   
        this.context = c;  
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(datalist!=null && datalist.size()>0){
            list = datalist; 
        }
    } 
  
    public void setListDataSetChanged(Bitmap bitmap){ 
    	if(bitmap!=null)
		list.add(new SoftReference<Bitmap>(bitmap));
    	notifyDataSetChanged();
    	
    }
    public int getCount() {  
        // TODO Auto-generated method stub  
        return list.size();  
    }  
  
    @Override  
    public Object getItem(int index) {  
  
        return list.get(index);  
    }  
  
    @Override  
    public long getItemId(int index) {  
        return index;  
    }  
  
    @Override  
    public View getView(int index, View convertView, ViewGroup parent) { 

        GridHolder holder;  
        if (convertView == null) {     
            convertView = mInflater.inflate(R.layout.expandablegrid_item, null);     
            holder = new GridHolder();  
            holder.appImage = (ImageView)convertView.findViewById(R.id.itemImage); 
            holder.appImage.setMaxHeight(Constants.m_screenWidth);
            holder.appImage.setMaxWidth(Constants.m_screenWidth);
//            holder.appImage.setLayoutParams(new RelativeLayout.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));
            convertView.setTag(holder);     
        }else{  
             holder = (GridHolder) convertView.getTag();
        }  
//        Bitmap bitmap = list.get(index).get();
        Bitmap bitmap = (Bitmap)list.get(index).get();
        if(bitmap!=null){
        	holder.appImage.setImageBitmap(bitmap);
        }else{
        	holder.appImage.setImageResource(R.drawable.icon);
        }
//        if (bitmap != null && bitmap.isRecycled() == false) {
//        	bitmap.recycle();
//        	bitmap = null;
//		}
        return convertView;  
    }  
    public static Bitmap drawableToBitmap(Drawable drawable) {  
        
        Bitmap bitmap = Bitmap  
                        .createBitmap(  
                                        drawable.getIntrinsicWidth(),  
                                        drawable.getIntrinsicHeight(),  
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                                        : Bitmap.Config.RGB_565);  
        Canvas canvas = new Canvas(bitmap);  
        //canvas.setBitmap(bitmap);  
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
        drawable.draw(canvas);  
        return bitmap;  
}  
    public void resletDataSetChanged(){
    	if(list!=null && list.size()>0)
    		for(SoftReference<Bitmap> Softbitmap : list){
    			Bitmap bitmap = Softbitmap.get();
    			if(bitmap !=null && bitmap.isRecycled())
    				bitmap.recycle(); 
				bitmap=null;
    		}
    		list.clear();
    		list = null; 
    }
    
}  
